;(function($){
	$.fn.extend({

		//创建菜单
		"createMenus":function(menuDatas){
			var menuObj = eval(menuDatas);
			var htmlStr = [];
			htmlStr.push('<ul class="metismenu" id="ems_menu">');
			
			$.each(menuObj,function(index,value){
				htmlStr = recurseCreate(htmlStr, $(value));
			});
			htmlStr.push("</ul>");
			$(".sidebar-nav").append(htmlStr.join(''));
			initMenu();
		}
	});

	function recurseCreate (htmlStr,childObj) {
		
		var child = childObj[0];
        var children = child['children'];

        if (children && children.length > 0) {
        	var badge = parseInt(child['badge']);
        	if (badge > 0) {
                htmlStr.push("<li><a class='has-arrow' href='#' aria-expanded='false'>" +
                    "<i class='"+child['micon']+"'></i>   "+child['name']+"<span class='ems_menu_badge'>"+badge+"</span></a>");
			}else {
                htmlStr.push("<li><a class='has-arrow' href='#' aria-expanded='false'>" +
                    "<i class='"+child['micon']+"'></i>   "+child['name']+"</a>");
			}
            htmlStr.push("<ul aria-expanded='false'>");
            $.each(children,function(index,value) {
                recurseCreate(htmlStr,$(value));
            });
            htmlStr.push("</ul>");
            htmlStr.push("</li>");
			return htmlStr;
		}else {
        	var url = child['url'];
            htmlStr.push("<li tab-id='"+child['id']+"'><a href="+url+" aria-expanded='false'>" + child['name']+"</a></li>");
            return htmlStr;
		}
	}
})(jQuery);
function initMenu() {
    //绑定点击事件
    $("#ems_menu").find("a").bind("click",function(event){
        //阻止冒泡事件
        event.stopPropagation();
        event.preventDefault();

        var url = "/ems" + $(this).attr("href");
        if (!url || url.indexOf("#" || "###") != -1) {
            return;
        }

        //操作iframe
        //onload='changeFrameHeight("+tabId+")'
        var tabId = $(this).parent().attr("tab-id");
        var ifmDiv = $(".ifm-div[tab-id="+tabId+"]");
        if (!ifmDiv || ifmDiv.length == 0) {
            $(".ems_main_content").append("<div class='ifm-div' tab-id='"+tabId+"'>" +
                "<iframe src='"+url+"' width='100%' height='100%' frameborder='0' id='page_content_"+tabId+"'></iframe></div>");
            $(".ifm-div[tab-id="+tabId+"]").siblings().filter(".ifm-div").hide();
            addTab($(this).text(),tabId);
        }else {
            ifmDiv.find("iframe").attr("src",url);
        }
        var thisTab = $(".ems_tab_div[tab-id="+tabId+"]");
        thisTab.trigger("click");
    });
}